package com.guassianblur

import android.graphics.RenderEffect
import android.graphics.Shader
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import android.widget.SeekBar
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.graphics.drawable.toBitmap
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            findViewById<ImageView>(R.id.imageView).setRenderEffect(
                RenderEffect.createBlurEffect(
                    200f,
                    200f,
                    Shader.TileMode.MIRROR
                )
            )
        }
        val timeStart = System.currentTimeMillis()
        val bitmap = resources.getDrawable(R.drawable.a2).toBitmap()
//        val gaussianBlur = GaussianBlur(this@MainActivity, bitmap.width, bitmap.height)
//        val gaussianBlur2 = GaussianBlurVT(this@MainActivity, bitmap.width, bitmap.height)
        val gaussianBlurAuto = GaussianBlurAuto(this@MainActivity, bitmap.width, bitmap.height)
//        val blurBitmap = gaussianBlur.applyGaussianBlur(bitmap)
//        val blurBitmap2 = gaussianBlur2.applyGaussianBlur(bitmap)
        gaussianBlurAuto.kernelSize = 10 * 2 + 1
        val blurBitmapAuto = gaussianBlurAuto.applyGaussianBlur(bitmap)
        val diff = System.currentTimeMillis() - timeStart
        Log.e("test", "onCreate, blur time is $diff")
        val blurImageView = findViewById<ImageView>(R.id.imageView2)
        blurImageView.setImageBitmap(blurBitmapAuto)
//        gaussianBlur.cleanup()
//        gaussianBlur2.cleanup()
//        gaussianBlurAuto.cleanup()

        findViewById<SeekBar>(R.id.seekBar).setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {

            }

            override fun onStartTrackingTouch(p0: SeekBar?) {
            }

            override fun onStopTrackingTouch(p0: SeekBar?) {
                val timeStart = System.currentTimeMillis()
                gaussianBlurAuto.kernelSize = p0!!.progress * 2 + 1
                val blurBitmapAuto = gaussianBlurAuto.applyGaussianBlur(bitmap)
                val diff = System.currentTimeMillis() - timeStart
                Log.e("test", "onStopTrackingTouch, blur time is $diff")
                blurImageView.setImageBitmap(blurBitmapAuto)
            }
        })
    }
}